package com.situ.car.controller;

import com.situ.car.pojo.entity.User;
import com.situ.car.pojo.query.UserQuery;
import com.situ.car.service.IUserService;
import com.situ.car.service.Impl.UserServiceImpl;
import com.situ.car.util.MyBeanUtils;
import com.situ.car.util.PageInfo;
import com.situ.car.util.Result;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserController extends BaseController{
    private IUserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "selectOrder":
                selectOrder(req, resp);
                break;
            case "exportExcel":
                exportExcel(req, resp);
                break;
            case "importExcel":
                importExcel(req, resp);
                break;
        }
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.importExcel");
        userService.importExcel(req);
        toJSON(resp, Result.ok("导入成功！"));
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.exportExcel");
        userService.exportExcel(resp);
    }


    private void selectOrder(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.selectOrder");
        List<User> list = userService.selectOrder();
        toJSON(resp, Result.ok(list));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.add");
        User user = MyBeanUtils.copyToBean(req, User.class);
        Integer accountCount = userService.selectByAccount(user);
        if (accountCount == 0) {
            userService.add(user);
            toJSON(resp, Result.ok("注册成功！"));
        } else {
            toJSON(resp, Result.error("账号已经存在！"));
        }
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.selectById");
        String id = req.getParameter("id");
        User user = userService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(user));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.update");
        User user = MyBeanUtils.copyToBean(req, User.class);
        int userPD = userService.update(user);
        if (userPD != 0) {
            toJSON(resp, Result.ok("修改成功！"));
        } else {
            toJSON(resp, Result.error("账号已经存在！"));
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        userService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        toJSON(resp, Result.ok("修改成功！"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.deleteById");
        String id = req.getParameter("id");
        userService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功!"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        if(!ObjectUtils.isEmpty(ids)) {
            userService.deleteAll(ids);
            toJSON(resp, Result.ok("删除成功！"));
        } else {
            toJSON(resp, Result.error("没有数据要删除！"));
        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserController.selectByPage");
        UserQuery userQuery = MyBeanUtils.copyToBean(req, UserQuery.class);
        PageInfo pageInfo = userService.selectByPage(userQuery);
        toJSON(resp, pageInfo);
    }
}
